<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-21 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use Jrk\Tree;
use think\facade\Db;

class Close extends CommonBaseModel implements Comm
{

    public function getAdminPageData($param = [], $order = 'id desc')
    {
        // TODO: Implement getAdminPageData() method.
        $where=[];
        if (isset($param['status']) && $param['status']!=-1) {
            $where[] = ['status','=',$param['status']];
        }
        if (isset($param['type']) && $param['type']) {
            $where[] = ['type','=',$param['type']];
        }
        if (isset($param['create_time']) && $param['create_time']) {
            $time = explode('~',$param['create_time']);
            $where[] = ['create_time','between',[strtotime($time[0].' 00:00:00'),strtotime($time[1].' 23:59:59')]];
        }
        $data = self::where($where)
            ->with(['merchant','task'])
            ->withCount(['taskFail' => 'fail_count'])
            ->order($order)
            ->page(PAGE)
            ->limit(LIMIT)
            ->select()->toArray();
        $count = self::where($where)->count("id");
        return parent::ajaxResult($data, $count);
    }

    /**
     * @param 获取上报任务列表
     * @return mixed|void
     */
    public function getTaskData($param=[],$order = 'id desc',$where=[]){

        if (isset($param['type']) && $param['type']) {
            $where[] = ['t.task_id','=',$param['type']];
        }
        if (isset($param['create_time']) && $param['create_time']) {
            $time = explode('~',$param['create_time']);
            $where[] = ['t.create_time','between',[strtotime($time[0].' 00:00:00'),strtotime($time[1].' 23:59:59')]];
        }
        $data = TaskList::
            alias('t')
            ->join('tang_task k','t.task_id=k.id')
            ->where($where)
            ->field('t.*,k.name as type')
            ->order($order)
            ->select()
            ->toArray();
        foreach ($data as $k=>$v){
            $data[$k]['no_report'] = empty($v['no_report'])? 0:count(explode(',',$v['no_report']));
        }

//        $task = Task::field('id')->select();
//        $data=[];
//        foreach ($task as $k=>$v){
//            $close =  self::where($where)->where('type',$v['id'])->group('start_time')->field('id,type,start_time,end_time')->order($order)->select()->toArray();
//            $close = $this->taskList($close);
//            $data = array_merge($data,$close);
//        }
//        //先将数据全部查询出来,收参数为了分页
        $page = $param['page']-1;
        $limit = $param['limit'];
////        $data = array_merge($close,$separate,$disinfection,$cleaning,$destroy,$gas);
//        array_multisort(array_column($data,'start_time'), SORT_DESC,$data);
        $count = count($data);
        $data = array_slice($data, $page*$limit, $limit);
        return parent::ajaxResult($data, $count);
    }

    public function taskList($gas){
        foreach ($gas as $key=>$val){
            $gas[$key]['type'] = Task::where('id',$val['type'])->value('name');
//            $merchantSum = Merchant::where('task_id','like','%'.$val['type'].'%')->count();
            $merchantSum = Merchant::whereFindInSet('task_id',$val['type'])->count();
            $gas[$key]['merchant_sum'] = $merchantSum;
            //已上报商户
            $merchant_up_sum  = Close::whereBetweenTime('create_time', $val['start_time'], $val['end_time'])->where('type',$val['type'])->count();
            $gas[$key]['merchant_up_sum']  = $merchant_up_sum;
            //未上报商户
            $gas[$key]['merchant_no_sum'] = $merchantSum-$merchant_up_sum;
            //审核通过
            $suggest = Close::whereBetweenTime('create_time', $val['start_time'], $val['end_time'])->where('status',1)->where('type',$val['type'])->count();
            $gas[$key]['pass'] = $suggest;
            //审核不通过
            $gas[$key]['nopass'] = Close::whereBetweenTime('create_time', $val['start_time'], $val['end_time'])->where('status',2)->where('type',$val['type'])->count();
        }
        return $gas;
    }
    public function delById($id)
    {
        // TODO: Implement delById() method.
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function merchant(){
        return $this->hasOne(Merchant::class,'id','merchant_id')->field('id,mobile');
    }
    public function task(){
        return $this->hasOne(Task::class,'id','type')->field('id,name');
    }
    public function taskFail(){
        return $this->hasMany(TaskFailLog::class,'task_id','id');
    }
}